//
//  TrainningType.swift
//  DaRings
//
//  Created by 尹琼 on 2022/11/16.
//

import UIKit
import CRPSmartRing
import HandyJSON
import MYModule

public enum TrainingType: Int, CaseIterable, HandyJSONEnum {
    
    case unknown = -1
    
    case gps_Run = 31
    case gps_Cycling = 32
    case gps_TrailRun = 33
    case gps_Onfoot = 34
    case gps_Walking = 30
    case walking = 0
    case running = 1
    case cycling = 2
    case skipping = 3
    case badminton = 4
    case basketball = 5
    case football = 6
    case climbing = 8
    case tennis = 9
    case rugby = 10
    case golf = 11
    case yoga = 12
    case fitness = 13
    case dancing = 14
    case baseball = 15
    case elliptical = 16
    case indoorCycling = 17
    case freeTraining = 18
    case rowing_machine = 19
    
    
}



public extension TrainingType {
    
    /// 所有运动类型
    static var allTypes: [TrainingType] {
        return allCases.filter { $0 != .unknown }
    }
    
    /// SDK type 转本类
    /// - Parameter type: CRPTrainingType
    /// - Returns: TrainningType
    static func type(of type: CRPTrainingType) ->TrainingType {
        
        guard let type = TrainingType(rawValue: type.rawValue) else {
            
            return .unknown
        }
        return type
    }
    
    
    // 转成SDK 使用的 type 类
    var CPRTrainningType: CRPTrainingType? {
        
        if self == .unknown {
            return nil
        }
        return CRPTrainingType(rawValue: self.rawValue)
    }
    
    // 是否是GPS类的运动
    var isGPS: Bool {
        if self == .gps_Run
            || self == .gps_Onfoot
            || self == .gps_Cycling
            || self == .gps_Walking
            || self == .gps_TrailRun {
            return true
            
        }
        return false
    }
    
    // 全局颜色
    var tintColor: UIColor {
        
        if isGPS {
            return UIColor.GPSTraining.theme
        }
        return UIColor.Training.theme
        
    }
    
    var iconName: String {
        
        switch self {
        case .gps_Run:
            
            return "ic_training_gps_02_running"
        case .gps_Cycling:
            
            return "ic_training_gps_03_cycling"
        case .gps_TrailRun:
            
            return "ic_training_gps_04_trail_running"
        case .gps_Onfoot:
            return "ic_training_gps_05_on_foot"
        case .gps_Walking:
            
            return "ic_training_gps_01_walking"
            
        case .walking:
            return "ic_training_01_walking"
        case .running:
            return "ic_training_02_running"
        case .cycling:
            return "ic_training_03_cycling"
        case .skipping:
            return "ic_training_04_skipping"
        case .badminton:
            return "ic_training_05_badminton"
        case .basketball:
            return "ic_training_06_basketball"
        case .football:
            return "ic_training_07_football"
        case .climbing:
            return "ic_training_08_climbing"
        case .tennis:
            return "ic_training_09_tennis"
        case .rugby:
            return "ic_training_10_rugby"
        case .golf:
            return "ic_training_11_golf"
        case .yoga:
            return "ic_training_12_yoga"
        case .fitness:
            return "ic_training_13_fitness"
        case .dancing:
            return "ic_training_14_dancing"
        case .baseball:
            return "ic_training_15_baseball"
        case .elliptical:
            return "ic_training_16_elliptical"
        case .indoorCycling:
            return "ic_training_17_indoor_cycling"
        case .freeTraining:
            return "ic_training_18_free_training"
        case .rowing_machine:
            return "ic_training_19_rowing_machine"
        case .unknown:
            return ""
        }
        
    }
    
    var title : String {
        
        switch self {
        case .gps_Run:
            return "gps_training_running".language
        case .gps_Walking:
            return "gps_training_walking".language
        case .gps_Cycling:
            return "gps_training_cycling".language
        case .gps_TrailRun:
            return "gps_training_trail_running".language
        case .gps_Onfoot:
            return "gps_training_on_foot".language
        case .walking:
            return "training_walking".language
        case .running:
            return "training_running".language
        case .cycling:
            return "training_cycling".language
        case .skipping:
            return "training_skipping".language
        case .badminton:
            return "training_badminton".language
        case .basketball:
            return "training_basketball".language
        case .football:
            return "training_football".language
        case .climbing:
            return "training_climbing".language
        case .tennis:
            return "training_tennis".language
        case .rugby:
            return "training_rugby".language
        case .golf:
            return "training_golf".language
        case .yoga:
            return "training_yoga".language
        case .fitness:
            return "training_fitness".language
        case .dancing:
            return "training_dancing".language
        case .baseball:
            return "training_baseball".language
        case .elliptical:
            return "training_elliptical".language
        case .indoorCycling:
            return "training_indoor_cycling".language
        case .freeTraining:
            return "training_free_training".language
            
        case .rowing_machine:
            return "training_rowing_machine".language
        case .unknown:
            return ""
        }
    }
    
}

public extension TrainingType {
    
    var contents: [TrainingTypeContent] {
        
        switch self {
        case .gps_Run:
            return TrainingTypeContent.allCases
            
        case .gps_Cycling:
            return TrainingTypeContent.time_heartRate_calorie_distance_pace
            
        case .gps_TrailRun,
                .gps_Onfoot,
                .gps_Walking,
                .walking,
                .running:
            return TrainingTypeContent.allCases
            
        case .cycling,
                .skipping,
                .yoga,
                .fitness,
                .dancing,
                .elliptical,
                .indoorCycling,
                .rowing_machine,
                .freeTraining:
            return TrainingTypeContent.time_heartRate_calorie
            
        case .badminton,
                .basketball,
                .football,
                .climbing,
                .tennis,
                .rugby,
                .golf,
                .baseball:
            
            return TrainingTypeContent.time_step_heartRate_calorie
            
            
        case .unknown:
            return []
        }
        
    }
    
}

//MARK: - 运动类型包含内容
public enum TrainingTypeContent: Int, CaseIterable {
    
    case unkown = 255
    case distance = 0
    case time = 1
    case pace = 2
    case calorie = 3
    case step = 10
    case heartRate = 11
    
    
}

extension TrainingTypeContent {
    
    // 是否可目标设置
    public var goalAble: Bool {
        
        switch self {
            
        case .distance, .time, .pace, .calorie:
            return true
            
        default:
            return false
        }
        
    }
    
    public var goalType: CRPSmartRing.CRPTrainingGoalType {
        
        switch self {
        case .distance:
            return .distance
        case .time:
            return .exerciseTime
        case .pace:
            return .step
        case .calorie:
            return .kcal
        case .step:
            return .none
        case .heartRate:
            return .none
        case .unkown:
            return .none
        }
    }
    
}

extension TrainingTypeContent {
    
    /// 主题
    public var title: String? {
        
        switch self {
            
        case .distance:
            return "unit_km".language
        case .time:
            return "gps_training_goal_setting_time_title".language
        case .pace:
            return "gps_training_goal_setting_pace_title".language
        case .calorie:
            return "gps_training_goal_setting_calories_title".language
            
        default:
            return nil
        }
        
    }
    
    
    
}

extension TrainingTypeContent {
    
    public static var time_step_heartRate_calorie: [TrainingTypeContent] {
        return TrainingTypeContent.allCases.filter {
            $0 == .time
            || $0 == .step
            || $0 == .heartRate
            || $0 == .calorie }
    }
    
    public static var time_heartRate_calorie: [TrainingTypeContent] {
        return TrainingTypeContent.allCases.filter {
            $0 == .time
            || $0 == .heartRate
            || $0 == .calorie }
    }
    
    public static var time_heartRate_calorie_distance_pace: [TrainingTypeContent] {
        return TrainingTypeContent.allCases.filter { $0 != .step }
    }
    
}

extension TrainingTypeContent {
    
   
    public func targets(type: UnitType) ->  [Double] {
        
        switch self {
            
        case .distance:
            
            if type == .imperial {
                
                return [3218, 4827, 9654, 16090, 21094, 42188]
                
            }
            
            return [3000, 5000, 10000, 15000, 21097.5, 42195]
        case .time:
            
            return [10 * 60, 20 * 60, 30 * 60, 60 * 60, 90 * 60, 120 * 60]
            
        case .pace:
            
            if type == .imperial {
                return [116, 113, 110, 108, 106, 105]
            }
            
            return [10, 8, 6, 5, 4, 3]
            
        case .calorie:
            return [150, 300, 350, 400, 500, 700]
            
        default:
            return []
        }
        
    }
    
    public var targetUnit: String? {
        
        switch self {
            
        case .distance:
            return Unit.sharedInstance.myType.baseDistance.name
        case .time:
            return "gps_training_goal_setting_time_unit".language
        case .pace:
            return Unit.sharedInstance.myType.basePace.name
        case .calorie:
            return "gps_training_goal_setting_calories_unit".language
            
        case .heartRate:
            return "gps_training_bpm".language
            
        default:
            return nil
        }
    }
    
    public var targetDescs: [String]? {
        
        switch self {
            
        case .distance:
            return ["", "", "", "",
                    "gps_training_goal_setting_distance_half_marathon".language, "gps_training_goal_setting_distance_marathon".language]
        case .time:
            
            return nil
            
        case .pace:
            return nil
            
        case .calorie:
            return ["gps_training_goal_setting_calories_toast".language,
                    "gps_training_goal_setting_calories_fries".language,
                    "gps_training_goal_setting_calories_chessecake".language,
                    "gps_training_goal_setting_calories_pizza".language,
                    "gps_training_goal_setting_calories_drumstick".language,
                    "gps_training_goal_setting_calories_hamburger".language]
            
        default:
            return nil
        }
        
    }
    
}

extension CRPSmartRing.CRPTrainingGoalType {
    
    public var trainingContentType: TrainingTypeContent {
        
        switch self {
            
        case .step:
            return .pace
        case .kcal:
            return .calorie
        case .exerciseTime:
            return .time
        case .distance:
            return .distance
        case .none:
            return .unkown
        @unknown default:
            return .unkown
        }
        
    }
    
}


